<?php
namespace app\common\model\xiluedu;


use think\db\Expression;
use think\Model;
use function fast\array_get;

class UserStudy extends Model
{
    protected $name="xiluedu_user_study";

    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    public function course(){
        return $this->belongsTo('Course','course_id','id',[],'left')->setEagerlyType(1);
    }



    public static function setPlayTime($params,$user_id){
        $course_id = array_get($params,'course_id');
        $course_file_id = array_get($params,'course_file_id');
        $type = array_get($params,'type');
        $data = [
            'type'          =>  $type,
            'user_id'       =>  $user_id,
            'course_id'     =>  $course_id,
            'course_file_id'=>  $course_file_id,
        ];
        $row = static::where('user_id',$user_id)->where('course_id',$course_id)->where('course_file_id',$course_file_id)->find();

        $view_time = array_get($params,'view_time');
        $data['view_time'] = ceil($view_time);
        //halt($data);
        if(!empty($row)){
            if($data['view_time'] > $data['view_time']){
                $result = $row->save($data);
            }
        }else{

            $result = self::create($data);
            Course::where('id',$course_id)->setInc('play_count',1);
        }
        return true;
    }

}